package com.ks.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.ks.base.BaseService;
import com.ks.dao.QuestiontypeDao;
import com.ks.entity.Questiontype;

@Service("questiontypeService")
public class QuestiontypeService<T> extends BaseService<T> {
    private final static Logger log = Logger.getLogger(QuestiontypeService.class);

    @Autowired
    private QuestiontypeDao<T> dao;

    public QuestiontypeDao<T> getDao() {
        return dao;
    }

    /**
     * 跳转到试题类型管理页面
     *
     * @param model
     */
    public void toQuestiontype(Model model) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("orderCondition", "");
        dataMap.put("mysqlQueryCondition", "");
        List<Questiontype> quetypeList = (List<Questiontype>) dao.queryByList(dataMap);
        if (!quetypeList.isEmpty()) {
            model.addAttribute("qutypeList", quetypeList);
        }
    }

    /**
     * 保存修改
     *
     * @param typeList
     * @return
     */
    public Map<String, Object> save(String typeList) {
        JSONArray jsonArray = JSONArray.fromObject(typeList);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "操作成功！";
        Integer status = 1;
        Object[] os = jsonArray.toArray();
        JSONObject jsonObj = null;
        ApplicationContext ac = null;
        for (int i = 0; i < os.length; i++) {
            jsonObj = JSONObject.fromObject(os[i]);
            ac = new FileSystemXmlApplicationContext("classpath:spring-context.xml");
            Questiontype questiontype = (Questiontype) ac.getBean("questiontype");
            if (null != jsonObj.get("id")) {
                String idStr = jsonObj.get("id").toString();
                if (StringUtils.isNotBlank(idStr)) {
                    questiontype.setId(Integer.parseInt(idStr));
                    questiontype.setName(jsonObj.get("name").toString());
                    questiontype.setCode(jsonObj.get("code").toString());
                    dao.updateBySelective(questiontype);
                } else {
                    questiontype.setName(jsonObj.get("name").toString());
                    questiontype.setCode(jsonObj.get("code").toString());
                    dao.add(questiontype);
                }
            }
        }

        resultMap.put("status", status);
        resultMap.put("msg", msg);

        return resultMap;
    }

    /**
     * 删除试题类型
     *
     * @param delId
     * @return
     */
    public Map<String, Object> deltype(String delId) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String msg = "删除成功！";
        Integer status = 1;
        dataMap.put("id", delId);
        dao.delete(dataMap);
        resultMap.put("status", status);
        resultMap.put("msg", msg);
        return resultMap;
    }

}
